package com.e3.basdoc.bank.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.bank.dao.BankDAO;
import com.e3.basdoc.bank.entity.BankEntity;
import com.e3.basdoc.bank.model.BankModel;
import com.e3.basdoc.bank.service.BankCacheManager;
import com.e3.basdoc.bank.service.BankService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;

public class BankServiceImpl extends SuperService implements BankService {
	
	private BankDAO bankDAO;
	
	public BankDAO getBankDAO(){
		if(bankDAO==null){
			bankDAO = DAOHelper.createDAOInstance(BankDAO.class);
		}
		return bankDAO;
	}

	public BankModel newBank(BankModel bankModel) throws ServiceException {
		validateBank(bankModel);
		checkBankExisted(bankModel);
		
		BankEntity entity = bankModelToEntity(bankModel, null);
		getBankDAO().persist(entity);
		
		bankModel.setId(entity.getId());
		
		BankCacheManager.INSTANCE.put(bankModel);
		
		return bankModel;
	}
	
	public BankModel modifyBank(BankModel bankModel) throws ServiceException{
		validateBank(bankModel);

		long id = bankModel.getId();
		BankEntity entity = getBankDAO().getById(BankEntity.class, id);
		if (entity == null) {
			return newBank(bankModel);
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(bankModel.getCode())) {
			checkBankExisted(bankModel);
		}

		bankModelToEntity(bankModel, entity);
		getBankDAO().merge(entity);
		
		BankCacheManager.INSTANCE.put(bankModel);
		return bankModel;
	}
	
	public void deleteBankByIds(List<Long> ids) throws ServiceException{
		List<Long> deepIds = getDeepChildrenIds(ids);
		getBankDAO().deleteBankByIds(deepIds);
		
		BankCacheManager.INSTANCE.remove(deepIds);
	}
	
	private List<Long> getDeepChildrenIds(List<Long> bankIds) throws ServiceException{
		if(bankIds==null||bankIds.isEmpty()){
			return null;
		}
		List<Long> ids = new ArrayList<Long>();
		for (long id : bankIds) {
			if(!ids.contains(id)){
				ids.add(id);
			}
		}
		List<Long> childrenIds = getBankDAO().getChildrenIds(bankIds);
		if(childrenIds!=null&&childrenIds.size()>0){
			List<Long> cIds = getDeepChildrenIds(childrenIds);
			for (Long id : cIds) {
				if(!ids.contains(id)){
					ids.add(id);
				}
			}
		}
		return ids; 
	}
	
	public void checkBankExisted(BankModel bankModel) throws ServiceException {
		if (bankModel == null) {
			return;
		}

		String code = bankModel.getCode();
		String name = bankModel.getName();
		
		StringBuffer msgs = new StringBuffer();
		long size = getBankDAO().getBankSizeByCode(code);
		if(size>0){
			msgs.append("编码为'" + code+"' ");
		}
		size = getBankDAO().getBankSizeByName(name);
		if (size>0) {
			msgs.append("名称为'" + name + "' ");
		}
		if(msgs.length()>0){
			msgs.append("已经存在，请设置新的编码和名称。");
			throw new ServiceException(msgs.toString());
		}
	}
	
	private void validateBank(BankModel bankModel) throws ServiceException{
		String code = bankModel.getCode();
		String name = bankModel.getName();

		if (code == null || code.trim().length() == 0 || name == null || name.trim().length() == 0) {
			throw new ServiceException("请设置编码和名称。");
		}
	}

	private BankEntity bankModelToEntity(BankModel bankModel, BankEntity entity) {
		BankEntity bankEntity = modelToEntity(BankEntity.class, bankModel, entity);

		bankEntity.setCode(bankModel.getCode());
		bankEntity.setName(bankModel.getName());
		bankEntity.setShortName(bankModel.getShortName());
		bankEntity.setParentId(bankModel.getParentId());
		bankEntity.setAddress(bankModel.getAddress());
		bankEntity.setRegionId(bankModel.getRegionId());
		bankEntity.setRemark(bankModel.getRemark());
		
		return bankEntity;
	}

}
